/**Spring Security配置文件，用于保护资源
 */
package com.example.demoauthinternalservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;

@Configuration
@EnableResourceServer
public class MyResourceConfig  extends ResourceServerConfigurerAdapter {
    RemoteTokenServices tokenServices() {
        RemoteTokenServices services = new RemoteTokenServices();
        services.setCheckTokenEndpointUrl("http://localhost:8770/oauth/check_token");
        services.setClientId("client_1");
        services.setClientSecret("mysecret");
        return services;
    }

    //配置服务保护规则
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/repository/**").hasRole("USER")//
            .anyRequest().authenticated();                      //配置所有请求访问控制，必须认证后才可以访问
//            .and()
//            .cors();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId("resource_1").tokenServices(tokenServices());           //本resource的id
        //...... 还可以有有其他的配置
    }
}
